package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.sms.api.feign.IConfigSmsFeign;
import com.ssy.lingxi.sms.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.sms.api.model.request.TemplateRequest;
import com.ssy.lingxi.system.common.MessageTemplateTypeEnum;
import com.ssy.lingxi.system.dao.CommonParametersRepository;
import com.ssy.lingxi.system.dao.TemplateParametersRepository;
import com.ssy.lingxi.system.dto.request.TemplateParametersAddRequest;
import com.ssy.lingxi.system.dto.request.TemplateParametersDetailsRequest;
import com.ssy.lingxi.system.dto.request.TemplateParametersDeteleRequest;
import com.ssy.lingxi.system.dto.request.TemplateParametersUpdateRequest;
import com.ssy.lingxi.system.dto.response.MessageTemplateTypeAllResponse;
import com.ssy.lingxi.system.entity.TemplateParameters;
import com.ssy.lingxi.system.service.TemplateParametersService;
import com.ssy.lingxi.system.utils.UserDetailsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TemplateParametersServiceImpl  implements TemplateParametersService {

    @Resource
    TemplateParametersRepository templateParametersRepository;
    @Resource
    CommonParametersRepository commonParametersRepository;
    @Resource
    UserDetailsUtil userDetailsUtil;
    @Resource
    IConfigSmsFeign iSmsServiceFeign;

    /**
     * 短信模板参数列表查询
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Override
    public Wrapper<PageData<TemplateParameters>> messageTemplateList(PageModel request) {
        Page<TemplateParameters> all = templateParametersRepository.findAll(PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending()));
        return Wrapper.success(new PageData<>(all.getTotalElements(),all.getContent()));
    }

    /**
     * 添加模板参数
     *
     * @param httpServletRequest
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Transactional
    @Override
    public Wrapper messageTemplateAdd(HttpServletRequest httpServletRequest, TemplateParametersAddRequest request) {
        TemplateParameters templateParameters1 = templateParametersRepository.queryByNameEquals(request.getName());
        if(null!=templateParameters1){
            return   Wrapper.fail("名字已经存在请修改后重试");
        }
        TemplateParameters templateParameters=new TemplateParameters();
        BeanUtils.copyProperties(request,templateParameters);
        templateParameters.setCreateTime(System.currentTimeMillis());
        templateParameters.setCreateRoleId(userDetailsUtil.getLoginUserDetails(httpServletRequest).getId());
        templateParametersRepository.save(templateParameters);

        TemplateRequest templateRequest = new TemplateRequest();
        templateRequest.setCode(request.getCode());
        templateRequest.setTemplateContent(request.getContent());
        templateRequest.setTemplateId(request.getTemplateId());
        templateRequest.setTemplateName(request.getName());
        iSmsServiceFeign.saveOrUpdateSmsTemplate(templateRequest);
        return Wrapper.success();
    }

    /**
     * 修改模板参数
     *
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Transactional
    @Override
    public Wrapper messageTemplateUpdate(TemplateParametersUpdateRequest request) {
        TemplateParameters templateParameters=new TemplateParameters();
        BeanUtils.copyProperties(request,templateParameters);
        templateParameters.setUpdateTime(System.currentTimeMillis());
        templateParametersRepository.save(templateParameters);

        TemplateRequest templateRequest = new TemplateRequest();
        templateRequest.setId(request.getId());
        templateRequest.setCode(request.getCode());
        templateRequest.setTemplateContent(request.getContent());
        templateRequest.setTemplateId(request.getTemplateId());
        templateRequest.setTemplateName(request.getName());
        iSmsServiceFeign.saveOrUpdateSmsTemplate(templateRequest);
        return Wrapper.success();
    }

    /**
     * 删除模板参数
     *
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Transactional
    @Override
    public Wrapper messageTemplateDelete(TemplateParametersDeteleRequest request) {
        templateParametersRepository.deleteById(request.getId());

        SimpleIdRequest simpleIdRequest = new SimpleIdRequest();
        simpleIdRequest.setId(request.getId());
        iSmsServiceFeign.deleteSmsTemplate(simpleIdRequest);
        return Wrapper.success();
    }

    /**
     * 查询短信详情
     *
     * @param request
     * @author wrc
     * @date 2020/6/17
     */
    @Override
    public Wrapper<TemplateParameters> messageTemplateDetails(TemplateParametersDetailsRequest request) {
        Optional<TemplateParameters> byId = templateParametersRepository.findById(request.getId());
       if(!byId.isPresent()){
           return Wrapper.failService("短信模板详情记录不存在");
       }
        return Wrapper.success(byId.get());
    }

    /**
     * 查询短信模板类型列表
     *
     * @author wrc
     * @date 2020/6/17
     */
    @Override
    public Wrapper<List<MessageTemplateTypeAllResponse>> messageTemplateTypeAll() {
        List<MessageTemplateTypeAllResponse> list=new ArrayList<>();
        MessageTemplateTypeAllResponse messageTemplateTypeAllResponse=null;
        MessageTemplateTypeEnum[] values = MessageTemplateTypeEnum.values();
        for (MessageTemplateTypeEnum messageTemplateTypeEnum:values) {
            messageTemplateTypeAllResponse=new MessageTemplateTypeAllResponse();
            messageTemplateTypeAllResponse.setId(messageTemplateTypeEnum.getCode());
            messageTemplateTypeAllResponse.setName(messageTemplateTypeEnum.getMessage());
            list.add(messageTemplateTypeAllResponse);
        }
        return Wrapper.success(list);
    }

    /**
     * 清空短信模板
     * @return
     */
    @Transactional
    @Override
    public Boolean clearSmsTemplate() {
        commonParametersRepository.deleteAllInBatch();
        templateParametersRepository.deleteAllInBatch();
        iSmsServiceFeign.clearSmsTemplate();
        return true;
    }


}
